<template>
  <el-dialog
    :title="!dataForm.bnId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    append-to-body>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="小说名称" prop="bnName">
      <el-input v-model="dataForm.bnName" placeholder="小说名称"></el-input>
    </el-form-item>
    <el-form-item label="小说分类" prop="bnCarteId">
      <el-input v-model="dataForm.bnCarteId" placeholder="小说分类"></el-input>
    </el-form-item>
    <el-form-item label="小说字数" prop="bnNumberWords">
      <el-input v-model="dataForm.bnNumberWords" placeholder="小说字数"></el-input>
    </el-form-item>
      <el-form-item label="小说榜单" prop="bnBooklistId">
        <el-input v-model="dataForm.bnBooklistId" placeholder="小说榜单"></el-input>
      </el-form-item>
      <el-form-item label="小说封面" prop="bnCover">
        <el-input v-model="dataForm.bnCover" placeholder="小说封面"></el-input>
      </el-form-item>
      <el-form-item label="小说摘要" prop="bnDigest">
        <el-input v-model="dataForm.bnDigest" placeholder="小说摘要"></el-input>
      </el-form-item>
    <el-form-item label="小说状态" prop="bnStatus">
      <el-input v-model="dataForm.bnStatus" placeholder="小说状态"></el-input>
    </el-form-item>
    <el-form-item label="小说评分" prop="bnScore">
      <el-input v-model="dataForm.bnScore" placeholder="小说评分"></el-input>
    </el-form-item>
    <el-form-item label="小说点评人数" prop="bnCommentNum">
      <el-input v-model="dataForm.bnCommentNum" placeholder="小说点评人数"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="bnCreationTime">
      <el-input v-model="dataForm.bnCreationTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="bnUpdateTime">
      <el-input v-model="dataForm.bnUpdateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          bnId: 0,
          bnName: '',
          bnCarteId: '',
          bnNumberWords: '',
          bnStatus: '',
          bnScore: '',
          bnCommentNum: '',
          bnCreationTime: '',
          bnUpdateTime: ''
        },
        dataRule: {
          bnName: [
            { required: true, message: '小说名称不能为空', trigger: 'blur' }
          ],
          bnCarteId: [
            { required: true, message: '小说分类不能为空', trigger: 'blur' }
          ],
          bnNumberWords: [
            { required: true, message: '小说字数不能为空', trigger: 'blur' }
          ],
          bnBooklistId: [
            { required: true, message: '小说榜单不能为空', trigger: 'blur' }
          ],
          bnCover: [
            { required: true, message: '小说封面不能为空', trigger: 'blur' }
          ],
          bnDigest: [
            { required: true, message: '小说摘要不能为空', trigger: 'blur' }
          ],
          bnStatus: [
            { required: true, message: '小说状态不能为空', trigger: 'blur' }
          ],
          bnScore: [
            { required: true, message: '小说评分不能为空', trigger: 'blur' }
          ],
          bnCommentNum: [
            { required: true, message: '小说点评人数不能为空', trigger: 'blur' }
          ],
          bnCreationTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          bnUpdateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.bnId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.bnId) {
            this.$http({
              url: this.$http.adornUrl(`/fiction/tbbookname/info/${this.dataForm.bnId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then((data) => {
              if (data && data.code === 200) {
                this.dataForm.bnName = data.tbBookName.bnName
                this.dataForm.bnCarteId = data.tbBookName.bnCarteId
                this.dataForm.bnNumberWords = data.tbBookName.bnNumberWords
                this.dataForm.bnBooklistId = data.tbBookName.bnBooklistId
                this.dataForm.bnCover = data.tbBookName.bnCover
                this.dataForm.bnDigest = data.tbBookName.bnDigest
                this.dataForm.bnStatus = data.tbBookName.bnStatus
                this.dataForm.bnScore = data.tbBookName.bnScore
                this.dataForm.bnCommentNum = data.tbBookName.bnCommentNum
                this.dataForm.bnCreationTime = data.tbBookName.bnCreationTime
                this.dataForm.bnUpdateTime = data.tbBookName.bnUpdateTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/fiction/tbbookname/${!this.dataForm.bnId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'bnId': this.dataForm.bnId || undefined,
                'bnName': this.dataForm.bnName,
                'bnCarteId': this.dataForm.bnCarteId,
                'bnNumberWords': this.dataForm.bnNumberWords,
                'bnBooklistId': this.dataForm.bnBooklistId,
                'bnCover': this.dataForm.bnCover,
                'bnDigest': this.dataForm.bnDigest,
                'bnStatus': this.dataForm.bnStatus,
                'bnScore': this.dataForm.bnScore,
                'bnCommentNum': this.dataForm.bnCommentNum,
                'bnCreationTime': this.dataForm.bnCreationTime,
                'bnUpdateTime': this.dataForm.bnUpdateTime
              })
            }).then((data) => {
              if (data && data.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
